<template>
  <div class="rightbox" v-cloak>
    <h1>用户新增</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item
          prop="name"
          label="用户名称"
          :rules="[
            { required: true, message: '请填写用户名称', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          prop="account"
          label="用户账号"
          :rules="[
            { required: true, message: '请填写用户账号', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordone"
          label="用户密码"
          :rules="[
            { required: true, message: '请填写用户密码', trigger: 'blur' }
          ]"
        >
          <el-input type="password" v-model="form.passwordone"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordtwo"
          label="确认密码"
        >
          <el-input type="password" v-model="form.passwordtwo"></el-input>
        </el-form-item>
        <el-form-item
          label="用户类型"
          prop="type"
        >
          <el-radio v-model="form.type" label="1">超级用户</el-radio>
          <el-radio v-model="form.type" label="0">普通用户</el-radio>
        </el-form-item>
        <el-form-item
          label="用户状态"
          prop="state"
        >
          <el-radio v-model="form.state" label="1">启用</el-radio>
          <el-radio v-model="form.state" label="-1">禁用</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
          <el-button  @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.passwordone) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '',
        account: '',
        passwordone: '',
        passwordtwo: '',
        state: '1',
        type: '1'
      },
      rules: {
        passwordtwo: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit: function (form) {
      var that = this
      that.$refs[form].validate((valid) => {
        if (valid) {
          that.$pub.open()
          axios.post('/gp/User/AddPost', qs.stringify({
            name: that.form.name,
            account: that.form.account,
            passwordone: that.form.passwordone,
            passwordtwo: that.form.passwordtwo,
            state: that.form.state,
            type: that.form.type
          })
          )
            .then((res) => {
              that.$pub.close()
              if (res.data.code === 0) {
                that.$pub.messageTwo(res.data.data, '提示')
                that.resetForm(form)
              } else {
                that.$pub.failcode(that, res.data.code, res.data.data)
              }
            })
            .catch((res) => {
              that.$pub.catchbox(that)
            })
        } else {
          return false
        }
      })
    },
    resetForm: function (form) {
      this.$refs[form].resetFields()
    }
  },
  mounted () {
  }
}
</script>
